package simtester.test;

import static org.junit.Assert.*;

import com.gmongo.GMongo
import com.mongodb.DBRef
import geb.junit4.GebReportingTest
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import simtester.test.pages.*
import simtester.test.Utils

class InstancesGuiTests extends GebReportingTest {
    static def testConfig
    static def db
    def testusername = 'Linh'
    def testpw = 'pass'
    def testcaseName = 'FunctionalDefaultTest'
    
    @BeforeClass
    static void setUpAll() {
        testConfig = new ConfigSlurper().parse(new File('grails-app/conf/SimTesterConfig.groovy').toURI().toURL())
        db = new GMongo().getDB(testConfig.test.dbname)
    }
    
    @AfterClass
    static void tearDownAll() {
        Utils.dropCollections(db)
    }
    
    @Before
    public void setUp() throws Exception {
        db.user << [_id: 2L, username: testusername, password: testpw.encodeAsMD5(), adminRole: false, dateCreated: new Date(), lastUpdated: new Date()]
        def user = db.user.findOne(username: testusername)
        println "User: $user"
        def testScript = 'test script'
        db.testCase << [_id: 1L, name: testcaseName, owner: new DBRef(null, 'user', 2L), script: testScript, dateCreated: new Date(), lastUpdated: new Date()]
    }
    
    @After
    public void tearDown() throws Exception {
        db.instance.remove([:])
        db.resultEntry.remove([:])
        db.testRun.remove([:])
        db.testCase.remove([:])
        db.user.remove([:])
        def dirs = new File(testConfig.test.env).listFiles()
        dirs.each {
            new AntBuilder().delete(dir: it)
        }
    }

    @Test
    void viewRunningInstances() {
        def tc = db.testCase.findOne(name: testcaseName)
        db.testRun << [_id: 1L, testcase: new DBRef(null, 'testCase', tc._id), status: 'completed', repeats: 1, dateCreated: new Date(), lastUpdated: new Date(), endDate: new Date()]

        def testCloudId = 'i-39555b7e'
        def testAmiId   = 'ami-1bd68a5e'
        def testType    = 'm1.large'
        def testStatus  = 'free'
        db.instance << [_id:         100L,
                        testrun:     new DBRef(null, 'testRun', 1L),
                        cloudId:     testCloudId,
                        amiId:       testAmiId,
                        type:        testType,
                        status:      testStatus,
                        //dnsName:     'ec2-50-18-146-28.us-west-1.compute.amazonaws.com', //TODO: Don't need yet
                        dateCreated: new Date()]
        
        to LoginPage
        assert at(LoginPage)
        auth.login(testusername, testpw)

        to InstancesPage
        assert at(InstancesPage)
        assert instancesTable.any {
            it.cloudId == testCloudId &&
            it.amiId == testAmiId &&
            it.type == testType &&
            it.status == testStatus &&
            it.dateCreated =~ /^\d\d\/\d\d\/\d{4} \d\d:\d\d:\d\d/
        }
    }
    
    @Test
    void noRunningInstances() {
        to LoginPage
        assert at(LoginPage)
        auth.login(testusername, testpw)
        
        to InstancesPage
        assert at(InstancesPage)
        assert $('.sttable tbody').text() == 'No running instances'
    }

}
